package com.hcms.system.controller;

import com.hcms.common.data.Tags;
import com.hcms.common.utils.Result;
import com.hcms.common.utils.StringUtil;
import com.hcms.system.domain.MenuDataDto;
import com.hcms.system.domain.MenuDo;
import com.hcms.system.domain.RoleDo;
import com.hcms.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RequestMapping("/system/menu")
@Controller
public class MenuController {

    String prefix = "system/menu";

    @Autowired
    MenuService menuService;

    @GetMapping()
    public String menu() {
        return prefix + "/menu";
    }

    @PostMapping("/list")
    @ResponseBody
    public Result list() {
        List<MenuDo> tree = menuService.tree();

        HashMap<String, Object> result = new HashMap<>();
        result.put("count", 1000);
        result.put("data", tree);

        return Result.ok(result);
    }

    @GetMapping("/add/")
    public String add(@RequestParam(name = "parentId", required = false) Integer parentId, Model model) {
        int parentIdInfo = 1;
        if (parentId != null && parentId > 0) {
            parentIdInfo = parentId;
        }
        model.addAttribute("parentId", parentIdInfo);
        model.addAttribute("markStatusData", new Tags().markStatusData());
        model.addAttribute("menuTypeData", new Tags().menuTypeData());

        return prefix + "/add";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result save(MenuDo menu) {
        menu.setCreateUser("admin");
        menu.setUpdateUser("admin");

        MenuDo parentInfo = menuService.get(menu.getParentId());
        String parentPath = parentInfo.getParentPath();
        parentPath = new StringUtil().stringDefault(parentPath, "");
        parentPath = parentPath + "," + menu.getParentId() + ",";
        parentPath = new StringUtil().removeRepeatSymbol(parentPath, ",");
        menu.setParentPath(parentPath);

        int count = menuService.save(menu);
        if (count > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @GetMapping("/edit/{id}/")
    public String edit(@PathVariable("id") int id, Model model) {
        MenuDo menuDo = menuService.get(id);
        model.addAttribute("menu", menuDo);
        model.addAttribute("markStatusData", new Tags().markStatusData());
        model.addAttribute("menuTypeData", new Tags().menuTypeData());

        return prefix + "/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(MenuDo menu) {
        menu.setUpdateUser("admin");

        if (menu.getId() > 1) {
            MenuDo parentInfo = menuService.get(menu.getParentId());
            String parentPath = parentInfo.getParentPath();
            parentPath = new StringUtil().stringDefault(parentPath, "");
            parentPath = parentPath + "," + menu.getParentId() + ",";
            parentPath = new StringUtil().removeRepeatSymbol(parentPath, ",");
            menu.setParentPath(parentPath);
        }

        int count = menuService.update(menu);
        if (count > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/tree")
    @ResponseBody
    public List<MenuDo> tree() {
        List<MenuDo> tree = menuService.tree();
        return tree;
    }

    @PostMapping("/menuinfo")
    @ResponseBody
    public List<MenuDataDto> getMenuData() {
        List<MenuDataDto> tree = menuService.getMenuData(9);
        return tree;
    }

    @PostMapping("/getByPermsName")
    @ResponseBody
    public MenuDo getByPermsName(String permsName) {
        MenuDo menuDo = menuService.getByPermsName(permsName);
        return menuDo;
    }
}
